package com.yumingjaing.stream;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @description:
 * @author: John
 * @version: FunctionTest.java, v1.0 2024年11月10日 16:21
 */
public class FunctionMethodTest {

    public static void main(String[] args) {
        List<String> objects = new ArrayList<>();
        objects.add("a");
        objects.add("b");
        objects.add("c");
        objects.add("d");
        objects.add("e");

        Function<String, String> objMap = new Function<>() {
            @Override
            public String apply(String s) {
                return "s+"+s;
            }
        };

        objects.stream().map(objMap).toList().forEach(System.out::println);
        System.out.println(">>>>>>>>>>>>>>>>>>>");
        objects.forEach(System.out::println);

        objects.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s+"-Consumer");
            }
        });

        boolean match = objects.stream().anyMatch(new Predicate<String>() {

            @Override
            public boolean test(String s) {
                return "a".equals(s);
            }
        });
        System.out.println(match);
        objects.stream().filter(startsWithB()).forEach(System.out::println);

        System.out.println("-------------");
        objects.stream().max(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.hashCode()-o2.hashCode();
            }
        }).ifPresent(System.out::println);
        System.out.println("--------------");

        List<String> aas = new ArrayList<String>();
        aas.add("a");
        aas.add("a");
        aas.stream().max(startsWithA()).ifPresent(System.out::println);

        System.out.println("===============");
    }
    // 定义一个返回Predicate<String>的方法
    private static Predicate<String> startsWithB() {
        return s -> s.startsWith("b");
    }

    private static Comparator<String> startsWithA() {
        return String::compareTo;
    }
}